﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using BPM.Foundation.PWDB;
namespace BPM.Foundation.PWService
{
    public partial interface IPowerService
    {
    }

    public partial class PowerService : IPowerService
    {
        //[2.1	连接]
        public  string  link(string data)
        {
            return "pid";
        }
        //-
        
        //[2.2	登陆]
        public bool? logon(string pid, string userNo, string password)
        {
            //[1]
            if( string.IsNullOrEmpty(userNo))
            {
                return false;
            }
            //-

            //[2]
            if (string.IsNullOrEmpty(password))
            {
                return false;
            }
            //-

            //[3]
            int v = PWDB.PW_User.Count(p => p.userNo == userNo && p.password == this.passwordMD5(password) && p.isFreeze == 0);

            if (v == 1)
            {
                return true;
            }
            else
            {
                return false;
            }
            //-

        }
        //-

        //[2.3	得到用户权限树]
        public PowerTree getUserPowerTree(string pid, string userNo)
        {
            PowerTree powerTree = null;
            //[1]
            if (string.IsNullOrEmpty(userNo))
            {
                return powerTree = null;
            }
            //-

            //[2]

            if (PWDB.PW_User.Count(p => p.userNo == userNo) != 1)
            {
                return powerTree = null;
            }
            else
            {
                List<Station> stations = new List<Station>();

                powerTree = new PowerTree();
                powerTree.user = PWDB.PW_User.SingleOrDefault(p => p.userNo == userNo);

                List<PW_DepartmentDutyUserRelation> pw_DepartmentDutyUserRelations = PWDB.PW_DepartmentDutyUserRelation.Where(p => p.userNo == userNo).ToList();

                foreach (var v in pw_DepartmentDutyUserRelations)
                {
                    var department = PWDB.PW_Department.SingleOrDefault(p => p.departmentNo == v.departmentNo);
                    var duty = PWDB.PW_Duty.SingleOrDefault(p => p.dutyNo == v.dutyNo);
                    Station station = new Station();
                    if (department != null && duty != null)
                    {
                        station.department = department;
                        station.duty = duty;
                        stations.Add(station);
                    }
                }
                powerTree.station = stations;
            }
            //-

            return powerTree;
        }
        //-
    }
}